var express = require('express');
var router = express.Router();
const filmService = require('../service/filmService');
/**
 * @swagger
 * /films/all:
 *    get:
 *      tags:
 *      - films
 *      summary: 获取所有电影
 *      parameters:
 *      - name: name
 *        in: query
 *        description: 姓名
 *        required: false
 *      - name: phone
 *        in: query
 *        description: 电话
 *        required: false
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.get('/all',async function(req, res, next) {
    res.send(await filmService.findAll());
});
/**
 * @swagger
 * /films/{id}:
 *    get:
 *      summary: 根据id查询
 *      tags:
 *       - films
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 电影id
 *        required: true
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.get('/:id',async function(req, res, next) {
    res.send(await filmService.findById(req.params.id));
});

/**
 * @swagger
 * /films:
 *    get:
 *      summary: 根据条件查找电影
 *      tags:
 *       - films
 *      parameters:
 *      - name: type
 *        in: query
 *        description: 搜索类型
 *        required: false
 *      - name: value
 *        in: query
 *        description: 搜索值
 *        required: false
 *      - name: current
 *        in: query
 *        description: 当前页
 *        required: false
 *      - name: size
 *        in: query
 *        description: 每页显示数
 *        required: false
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.get('/',async function(req, res, next) {
    let {type,value,current,size} = req.query;
    let searchObj = {};
    if(type){
        searchObj[type] = {$regex:value};
    }
    let data = await filmService.find(searchObj,{current:current||1,size:size||3});
    res.send(data);
});

module.exports = router;
